package main

import (
	"fmt"
	"time"

	"github.com/user/rxgo"
)

func main() {
	fmt.Print("Debounce: ")
	rxgo.Just(1, 8, 3, 4, 2, 0, 2, 6).Debounce(2).Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(" ")
	})
	fmt.Println()
	fmt.Println()

	fmt.Print("Distinct: ")
	rxgo.Just(1, 8, 3, 4, 2, 0, 2, 6).Distinct().Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(" ")
	})
	fmt.Println()
	fmt.Println()

	fmt.Print("ElementAt 3: ")
	rxgo.Just(1, 8, 3, 4, 2, 0, 2, 6).ElementAt(3).Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(" ")
	})
	fmt.Println()
	fmt.Println()

	fmt.Print("First: ")
	rxgo.Just(1, 8, 3, 4, 2, 0, 2, 6).First().Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(" ")
	})
	fmt.Println()
	fmt.Println()

	//filter
	fmt.Print("Filter value < 4: ")
	res := []int{}
	rxgo.Just(1, 8, 3, 4, 2, 0, 2, 6).Filter(func(x int) bool {
		return x < 4
	}).Subscribe(func(x int) {
		res = append(res, x)
	})
	for i := range res {
		fmt.Print(res[i])
		fmt.Print(" ")
	}
	fmt.Println()
	fmt.Println()

	fmt.Print("IgnoreElements: ")
	rxgo.Just(1, 8, 3, 4, 2, 0, 2, 6).IgnoreElements().Subscribe(func(x int) {
		fmt.Print(x)
	})
	fmt.Println()
	fmt.Println()

	fmt.Print("Last: ")
	rxgo.Just(1, 8, 3, 4, 2, 0, 2, 6).Last().Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(" ")
	})
	fmt.Println()
	fmt.Println()

	fmt.Print("Sample: ")
	observableP := make(chan interface{})
	go func() {
		rxgo.Just(1, 2).Map(func(x int) int {
			switch x {
			case 1:
				time.Sleep(10 * time.Millisecond)
			case 2:
				time.Sleep(5 * time.Millisecond)
			default:
				time.Sleep(10 * time.Millisecond)
			}
			return x
		}).Subscribe(func(x int) {
			observableP <- x
		})
	}()
	rxgo.Just(1, 8, 3, 4, 2, 0, 2, 6).Map(func(x int) int {
		time.Sleep(3 * time.Millisecond)
		return x
	}).Sample(observableP).Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(" ")
	})
	fmt.Println()
	fmt.Println()

	fmt.Print("Skip 2: ")
	rxgo.Just(1, 8, 3, 4, 2, 0, 2, 6).Skip(2).Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(" ")
	})
	fmt.Println()
	fmt.Println()

	fmt.Print("SkipLast 2: ")
	rxgo.Just(1, 8, 3, 4, 2, 0, 2, 6).SkipLast(2).Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(" ")
	})
	fmt.Println()
	fmt.Println()

	fmt.Print("Take 4: ")
	rxgo.Just(1, 8, 3, 4, 2, 0, 2, 6).Take(4).Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(" ")
	})
	fmt.Println()
	fmt.Println()

	fmt.Print("TakeLast 3: ")
	rxgo.Just(1, 8, 3, 4, 2, 0, 2, 6).TakeLast(3).Subscribe(func(x int) {
		fmt.Print(x)
		fmt.Print(" ")
	})
	fmt.Println()
	fmt.Println()

}
